mod window;
mod keymap;
mod data;

use std::env;
use std::path::Path;
use window::*;
use ncurses::*;
use pyo3::{marker::Python, types::PyDict};
use ho_string::HoString;
use ho_file::HoFile;
use keymap::*;
use data::ContentChar;

fn set_color(ui: &Vec<Window>) {
    start_color();
    for value in ui {
        init_pair(
            (*value).color_id,
            (*value).color_fg,
            (*value).color_bg
        );
    }
}

fn main() {
    Python::with_gil(|py: Python| {
        let args: Vec<String> = env::args().collect();
        let version: String = String::from("v0.0.1");
        let exec_file_name: &String = args.get(0).unwrap();
        let mut is_open_file: bool = false;
        let mut file_name: &String = &String::new();
        if args.len() >= 3 {
            println!(r#"{} [file]
file    The file that needs to be opened.
{} {}"#, exec_file_name, exec_file_name, version);
            return;
        } else if args.len() == 2 {
            file_name = args.get(1).unwrap();
            let file: &Path = Path::new(file_name);
            if !file.is_file() {
                println!("{} is not a file", *file_name);
                return;
            }
            is_open_file = true;
        }
        let locals: &PyDict = PyDict::new(py);
        let config: HoString = HoFile::read_file_all("plugins/config.py", "");
        py.run(config.to_str(), None, Some(locals)).unwrap();
        let file_explorer_width: f64 = locals.get_item("file_explorer_width").unwrap().unwrap().extract::<f64>().unwrap();
        initscr();
        raw();
        keypad(stdscr(), true);
        noecho();
        curs_set(CURSOR_VISIBILITY::CURSOR_VISIBLE);
        let ui: Vec<Window> = init_window(file_explorer_width);
        let file_explorer: &Window = ui.get(0).unwrap();
        let editor: &Window = ui.get(1).unwrap();
        let mut content: HoString = HoString::new();
        let mut content_chars: Vec<Vec<ContentChar>> = Vec::new();
        set_color(&ui);
        if ui.len() < 2 {
            for i in &ui {
                delwin((*i).win);
            }
            endwin();
            println!("Error: not found editor control");
            return;
        }
        mv(1, (*file_explorer).width + 1);
        if is_open_file {
            content = HoFile::read_file_all((*file_name).as_str(), "");
            content_chars = add_content(&ui, &content);
            wmove((*editor).win, 1, 1);
        }
        draw_border(&ui);
        let mut lines: Vec<HoString> = Vec::new();
        let mut line_index: isize = -1;
        let mut line_max: isize = 0;
        for c in (&content).to_string().chars() {
            if line_index < line_max {
                lines.push(HoString::new());
                line_index += 1;
            }
            if c == 13 as char {
                lines.push(HoString::new());
                line_max += 1;
                line_index += 1;
            } else {
                lines[line_index as usize].push_char(c);
            }
        }
        set_keymap(&ui, content_chars);
        endwin();
    });
}
